package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.entity.chapter.ChapterVo;
import com.atguigu.eduservice.entity.chapter.VideoVo;
import com.atguigu.eduservice.mapper.EduChapterMapper;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduVideoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-06-01
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {
    @Autowired
    private EduVideoService videoService;

    //根据课程id查询课程大纲列表
    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {
        //1 根据课程id查询课程里面所有的章节
        QueryWrapper<EduChapter> chapterWrapper = new QueryWrapper<>();
        chapterWrapper.eq("course_id", courseId);
        List<EduChapter> chapterList = baseMapper.selectList(chapterWrapper);

        //2 根据课程id查询课程里面所有的小节
        QueryWrapper<EduVideo> videoWrapper = new QueryWrapper<>();
        videoWrapper.eq("course_id", courseId);
        List<EduVideo> videoList = videoService.list(videoWrapper);

        //创建list集合，用于最终封装数据
        ArrayList<ChapterVo> finalList = new ArrayList<>();
//3 遍历查询章节list集合进行封装
        for (EduChapter eduChapter : chapterList) {
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(eduChapter, chapterVo);

            finalList.add(chapterVo);

//            ArrayList<VideoVo> videoVos = new ArrayList<>();
//            for (EduVideo eduVideo : videoList) {
//                VideoVo videoVo = new VideoVo();
//                BeanUtils.copyProperties(eduVideo, videoVo);
//                videoVos.add(videoVo);
//            }

            //创建集合，用于封装章节的小节
            ArrayList<VideoVo> videoVos = new ArrayList<>();

            for (EduVideo eduVideo : videoList) {
                //判断小节的ChapterId与章节的id是狗一样
                if (eduVideo.getChapterId().equals(chapterVo.getId())) {
                    //进行封装
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(eduVideo, videoVo);
                    //放到小姐封装集合
                    videoVos.add(videoVo);
                }
            }
            //把封装之后的小节集合，放到小节对象里面
            chapterVo.setChildern(videoVos);
        }


        return finalList;
    }

//    //删除章节的方法 当有小节时不删除
//    @Override
//    public Boolean deleteChapter(String chapterId) {
//        //根据chapterId查询是否有小节，有就不删除
//        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
//        wrapper.eq("chapter_id", chapterId);
//        int count = videoService.count(wrapper);
//        if (count > 0) {//有小节，不能删除
//            throw new GuiliException(20001, "不能删除章节");
//        } else {
//            //删除章节
//            int result = baseMapper.deleteById(chapterId);
//            return result > 0;//删除成功result=1，
//        }
//    }
        //删除章节的方法 并删除小节
    @Override
    public Boolean deleteChapter(String chapterId) {
        //根据chapterId查询是否有小节，有就删除
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id", chapterId);
        List<EduVideo> eduVideos = videoService.list(wrapper);
        for (EduVideo eduVideo : eduVideos) {
            videoService.removeById(eduVideo.getId());
        }
            //删除章节
            int result = baseMapper.deleteById(chapterId);
            return result > 0;//删除成功result=1，
        }
    //根据课程id删除章节
    @Override
    public void removeChapterByCourseId(String courseId) {
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        baseMapper.delete(wrapper);

    }
}